Can you please learn to use <code> tags in your posts? They will render your code legible, at least.
Ok, the code you posted is this:
#!/usr/bin/perl -w
use strict;
my $input='';
my @raw=();
while(defined ($input=<>)){
chomp($input);
@raw=$input;
}
print "@raw\n";
Which means you didn't follow any of the suggestions I provided earlier -- kinda demotivates me from continuing, but I'll proceed.
Modify your code to look like this:
#!perl
use strict;
use warnings;
my @raw;
while( my $input = <> ) {
chomp $input;
push @raw, $input;
}
print "@raw\n";
Now I'll explain the differences.
- #!perl instead of #!/usr/bin/perl -w: Strawberry Perl runs under Windows. Windows uses your PATH environment variable and your file extension (.pl) to determine what to do when you type myscript.pl. That means the shebang line is only really used for command-line switches. But just for clarity we'll leave it in there, minus the path information. Furthermore, the path information you were using is probably invalid (it's unlikely that your Perl interpreter is installed at C:/usr/bin/perl. See perlwin32 for further details.
- use warnings; The warnings pragma is lexically scoped, whereas the -w flag is global, which may result in unanticipated warning messages from modules. So I didn't use the -w flag, and did use the 'warnings' pragma. See perlrun and warnings for additional information.
- my $input has been moved to within the while(){} loop's scope; you're not using it at the broader scope, so there's no reason to scope it more broadly than that. And I've omitted the = '' part, because it was unnecessary. See perlsyn and perlsub for details on while(){} and lexical (my) scoping.
- I removed the = (); from my @raw = ();. It's unnecessary; the array will be empty to begin with. This isn't an auto from C.
- I removed the defined function call. It's the default behavior of the construct we're using. See the <> operator section of perlop.
- push @raw, $input; instead of @raw = $input;. When you assign a scalar variable ($input) to an array (@raw) you clobber whatever contents was already in the array, and assign the new value to $array[0] (the first element). I'm sure this is not what you intended. You intended to build an array by adding $input as the next element on each iteration. That's what push does.</c>
If you don't need the entire file to be held in memory at the same time, you could rewrite the above like this:
#!perl
use strict;
use warnings;
while( <> ) {
chomp;
print;
}
print $/;
You were seeing no output because the last line of your file was probably blank. Since your original code only kept the last line (throwing away all the rest), the output appeared blank.
Now for how to invoke the script. If myscript.pl inputfile doesn't work out (perhaps your system isn't configured to recognize the .pl extension), you can instead type perl myscript.pl inputfile. But do ensure that your PATH environment variable points to the perl.exe executable.
|