Perl

Moderators: Jonathan
Number of threads: 1236
Number of posts: 3605

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
New to Perl and stuck...Help Posted by mikeruth on 18 Dec 2003 at 1:11 PM
This message was edited by mikeruth at 2003-12-18 13:12:55

This message was edited by mikeruth at 2003-12-18 13:12:10

I am on my second day of Perl programming and I am stuck. The program
below will read a one line, 1 to 2 M file, test it, and, print some counts to another file. I cannot get past the open...infile.

Help! I need to get this done soon (and with "use strict").

Where did all of my spaces go???

========================= The Code ===========================

#!/opt/utils/5bin/perl5.001 -w

# We have two versions. Make sure to use the lastest.

require 5.001;
use strict;

if ( @ARGV < 2 )
{
die "\nUsage: $0 <infile>\n\nStopped";
}

my $infile = shift @ARGV;
my $outfile = shift @ARGV;

open( my $infile_handle, "< $infile" ) or
die "\nCan't open input file \"$infile\".\n",
"Error: $!.\n",
"Stopped";

open( my $outfile_handle, "> $outfile" ) or
die "\nCan't open output file \"$outfile\".\n",
"Error: $!.\n",
"Stopped";

my $text = join( '', <$infile_handle> );
my $test_len = length( $test );
print "\n" , $test_len, "\n";

close $infile_handle;

chomp $text;

#
# process the line and print results...
#

close $outfile_handle;

exit ( 0 );





Report
Re: New to Perl and stuck...Help Posted by Jonathan on 18 Dec 2003 at 1:53 PM
: I am on my second day of Perl programming and I am stuck. The ]
: program below will read a one line, 1 to 2 M file, test it, and,
: print some counts to another file. I cannot get past the
: open...infile.
:
: Help! I need to get this done soon (and with "use strict").
:
: Where did all of my spaces go???
:
: ========================= The Code ===========================
:
: #!/opt/utils/5bin/perl5.001 -w
:
: # We have two versions. Make sure to use the lastest.
:
: require 5.001;
: use strict;
:
: if ( @ARGV < 2 )
: {
: die "\nUsage: $0 <infile>\n\nStopped";
: }
:
: my $infile = shift @ARGV;
: my $outfile = shift @ARGV;
:
: open( my $infile_handle, "< $infile" ) or
: die "\nCan't open input file \"$infile\".\n",
: "Error: $!.\n",
: "Stopped";
Don't see an immediate problem here, but you are using an older version of Perl. This means it may like you to declare $infile_handle before the open statement, e.g.:-

my $infile_handle;
open $infile_handle, "< $infile" or ...blah...;

: open( my $outfile_handle, "> $outfile" ) or
: die "\nCan't open output file \"$outfile\".\n",
: "Error: $!.\n",
: "Stopped";
And maybe the same there.

: my $text = join( '', <$infile_handle> );
: my $test_len = length( $test );
Should that not be:-
my $test_len = length( $text );

: print "\n" , $test_len, "\n";
:
: close $infile_handle;
:
: chomp $text;
:
: #
: # process the line and print results...
: #
:
: close $outfile_handle;
:
: exit ( 0 );

Looks OK besides that. FYI, it's good to enclose code in code and /code tags - tags are put in square brackets. Then the spacing is retained.

Hope this helps, if not please post the output of perl -c thescript.pl, where thescript.pl is your script. That or run it and post the warnings that you're given.

Jonathan

###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Report
Re: New to Perl and stuck...Help Posted by mikeruth on 18 Dec 2003 at 2:19 PM
This message was edited by mikeruth at 2003-12-18 14:28:27

This message was edited by mikeruth at 2003-12-18 14:25:13

I made the changes that you suggested (thanks for catching the "test"
error). The new code is:

my $infile_handle;
open( $infile_handle, "< $infile" ) or ...

When run I get:

Can't use an undefined value as a symbol reference at edijd110.pl line 24. <--- the open above

With -c I get:

edijd110.pl syntax OK


: : open( my $infile_handle, "< $infile" ) or
: : die "\nCan't open input file \"$infile\".\n",
: : "Error: $!.\n",
: : "Stopped";
: Don't see an immediate problem here, but you are using an older version of Perl. This means it may like you to declare $infile_handle before the open statement, e.g.:-
:
: my $infile_handle;
: open $infile_handle, "< $infile" or ...blah...;
:
: : open( my $outfile_handle, "> $outfile" ) or
: : die "\nCan't open output file \"$outfile\".\n",
: : "Error: $!.\n",
: : "Stopped";
: And maybe the same there.
:
: Looks OK besides that. FYI, it's good to enclose code in code and /code tags - tags are put in square brackets. Then the spacing is retained.
:
: Hope this helps, if not please post the output of perl -c thescript.pl, where thescript.pl is your script. That or run it and post the warnings that you're given.
:
: Jonathan
:
: ###
: for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
: (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
: /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
:
:





Report
Re: New to Perl and stuck...A solution?? Posted by mikeruth on 18 Dec 2003 at 2:43 PM
I found a solution but I do not know why it works.

I deleted the line "my $infile_handle;".
I changed all occurences of "$infile_handle" to "INFILE_HANDLE".

Why does this work?



: This message was edited by mikeruth at 2003-12-18 14:28:27

: This message was edited by mikeruth at 2003-12-18 14:25:13

: I made the changes that you suggested (thanks for catching the "test"
: error). The new code is:
:
: my $infile_handle;
: open( $infile_handle, "< $infile" ) or ...
:
: When run I get:
:
: Can't use an undefined value as a symbol reference at edijd110.pl line 24. <--- the open above
:
: With -c I get:
:
: edijd110.pl syntax OK
:
:
: : : open( my $infile_handle, "< $infile" ) or
: : : die "\nCan't open input file \"$infile\".\n",
: : : "Error: $!.\n",
: : : "Stopped";
: : Don't see an immediate problem here, but you are using an older version of Perl. This means it may like you to declare $infile_handle before the open statement, e.g.:-
: :
: : my $infile_handle;
: : open $infile_handle, "< $infile" or ...blah...;
: :
: : : open( my $outfile_handle, "> $outfile" ) or
: : : die "\nCan't open output file \"$outfile\".\n",
: : : "Error: $!.\n",
: : : "Stopped";
: : And maybe the same there.
: :
: : Looks OK besides that. FYI, it's good to enclose code in code and /code tags - tags are put in square brackets. Then the spacing is retained.
: :
: : Hope this helps, if not please post the output of perl -c thescript.pl, where thescript.pl is your script. That or run it and post the warnings that you're given.
: :
: : Jonathan
: :
: : ###
: : for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
: : (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
: : /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
: :
: :
:
:
:
:
:
:

Report
Re: New to Perl and stuck...A solution?? Posted by Jonathan on 18 Dec 2003 at 2:56 PM
: I found a solution but I do not know why it works.
:
: I deleted the line "my $infile_handle;".
: I changed all occurences of "$infile_handle" to "INFILE_HANDLE".
That was going to be my suggestion, but I went to grab some whisky before replying and you beat me to it.

: Why does this work?
In older versions of Perl 5 it was not possible to store a file handle in a scalar. 5.005 qualifies as "older".

Jonathan


###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Report
Re: New to Perl and stuck...A solution?? Posted by mikeruth on 18 Dec 2003 at 3:01 PM
Thank you for your help and time.


: : I found a solution but I do not know why it works.
: :
: : I deleted the line "my $infile_handle;".
: : I changed all occurences of "$infile_handle" to "INFILE_HANDLE".
: That was going to be my suggestion, but I went to grab some whisky before replying and you beat me to it.
:
: : Why does this work?
: In older versions of Perl 5 it was not possible to store a file handle in a scalar. 5.005 qualifies as "older".
:
: Jonathan
:
:
: ###
: for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
: (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
: /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
:
:




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.