Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

weird begginer's problem...

by Arien0611 (Novice)
on Sep 06, 2012 at 16:10 UTC ( #992131=perlquestion: print w/replies, xml ) Need Help??
Arien0611 has asked for the wisdom of the Perl Monks concerning the following question:

hello monks. i have a problem with a subroutine of mine. i dont know if it something wrong with its syntax, and googling it, wasnt much of a help. so, i have this subroutine:
sub exractSongTitlesFromCueFile{ my ($cue_file_to_parse) = @_; my @album_info; my $w = 0; open(CUEFILEFOUND, $cue_file_found) || die "Error 003: Could not o +pen cue file named: $cue_file_found! $!\n"; while(<CUEFILEFOUND>){ if($_ =~ /\s+TITLE "(.*)".*/){ $album_info[$w] = $1 || die "Error 004: Could assign song +title to array! $!\n"; # print "$1\n"; $w++ || die "Error 005: Could not increment array pointer +$!\n"; } } close(CUEFILEFOUND); return (@album_info); } </code and running the script, would give me this output: <code> Error 005: Could not increment array pointer Inappropriate ioctl for d +evice
i dont understand what i m doing wrong. i mean, i just want to increment my variable... thanks for your time though, and sorry if my question is a tad stupid. i just dont understand the problem, and i cant search for it somewhere. so any help would be appreciated.

Replies are listed 'Best First'.
Re: weird begginer's problem...
by choroba (Bishop) on Sep 06, 2012 at 16:16 UTC
    You start with $w == 0. You post-increment it, which means before changing its value, you perform the || die test. Therefore, 0 || die goes the "or" way and dies. Since there was no system failure immediately before the death, the value of $! is meaningless, as documented in Perl Predefined Variables / $OS_ERROR.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: weird begginer's problem...
by hbm (Hermit) on Sep 06, 2012 at 16:17 UTC


    perl -e "$w=0; $w++||die" Died at -e line 1. perl -e "$w=0; ++$w||die" # no error
      hm, i knew it would something stupid. i m stupid. i should go and farm some cows, instead of programming. thank you for the help though guys. i appreciate it.

        I suspect that the rate of incredibly stupid mistakes doesn't actually decrease as you gain experience. We just get less incredulous as we go.

Re: weird begginer's problem...
by MidLifeXis (Monsignor) on Sep 06, 2012 at 16:23 UTC

    A preference as well, although it may save you some trouble later:

    foo() || die "bar"


    foo() or die "bar"

    The || is of higher precedence than or. Using or (at least in the action or die pattern) can help avoid some sneaky precedence bugs.

    Update: Added example below

    perl -e 'print 1 - 1 || die "hello"'


    perl -e 'print 1 - 1 or die "hello"'


Re: weird begginer's problem...
by Khen1950fx (Canon) on Sep 06, 2012 at 16:29 UTC
    You'll need to declare $cue_file_found, and you'll also need to use strictures. I ran this and got no errors:
    #!/usr/bin/perl use strict; use autodie; use warnings; sub exractSongTitlesFromCueFile { my ( $cue_file_to_parse ) = @_; my @album_info; my $w = 0; my $cue_file_found = '/tmp/cue/cue_file'; open( CUEFILEFOUND, '<', $cue_file_found ) or die "Could not open cue file named: $cue_file_found! $!\n"; while (<CUEFILEFOUND>) { if ( $_ =~ /\s+TITLE "(.*)".*/ ) { $album_info[$w] = $1 || die "Error 004: Couldn't assign song title to array! $!\n"; $w++ || die "Error 005: Could not increment array pointer $!\n"; } } close(CUEFILEFOUND); return (@album_info); }

      Does not seem correct, just based on reading, as this version has the same root-cause issue as the first: the $w++ starts out at zero, causing the die to fire the first time through the loop where the TITLE matches.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://992131]
Approved by Ratazong
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2018-01-19 00:26 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (215 votes). Check out past polls.