On Sat, Feb 18, 2012 at 12:25 AM, Shawn H Corey <shawnhco...@gmail.com> wrote:
> On 12-02-17 10:32 AM, lina wrote:
>>
>> #!/usr/bin/perl
>>
>> use warnings;
>> use strict;
>>
>> open FILE, "<try.xpm" or die $!;
>>
>> my @line =<FILE>;
>>
>> while (<FILE>) {
>>        print $_;
>> }
>>
>> strangely it print me nothing out,
>
>
> Try:
>
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> # place the file name in a variable
> # so that it can be easier changed later
> my $file = "try.xpm";
>
> # open with a lexically-scoped file handle
> # to avoid name collisions with other file handles
> open my $fh, '<', $file or die "could not open $file: $!\n";
>
> # read each line into a appropriately named variable
> while( my $xpm_line = <$fh> ){
>
>    # for demo purposes; this will be replaced later with more code
>    print $xpm_line;
>
> } # end while <$fh>

Thanks, the first version was the product when I googled about how to
open and read a file in perl.

Now:
#!/usr/bin/perl

use warnings;
use strict;

my $file = "try.xpm";

open my $fh, "<$file" or die "could not open $file: $!\n";

while (my $xpm_file = my <$fh>) {
        print $xpm_file;
}


It showed me:

$ ./translate.pl
syntax error at ./translate.pl line 10, near "my <$fh>"
Global symbol "$xpm_file" requires explicit package name at
./translate.pl line 11.
Execution of ./translate.pl aborted due to compilation errors.

Something is related to the strict again?

How do you save time when you build a new script by avoiding typing the
Now:
#!/usr/bin/perl

use warnings;
use strict;

head -4 old.pl > new.pl ?

Thanks,

>
> __END__
>
> --
> Just my 0.00000002 million dollars worth,
>  Shawn
>
> Programming is as much about organization and communication
> as it is about coding.
>
> It's Mutual Aid, not fierce competition, that's the dominate
> force of evolution.  Of course, anyone who has worked in
> open source already knows this.
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to