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.

  1. #!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.
  2. 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.
  3. 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.
  4. I removed the = (); from my @raw = ();. It's unnecessary; the array will be empty to begin with. This isn't an auto from C.
  5. I removed the defined function call. It's the default behavior of the construct we're using. See the <> operator section of perlop.
  6. 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.


Dave


In reply to Re^6: Input files <> by davido
in thread Input files <> by ostra

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.