girish_01 has asked for the wisdom of the Perl Monks concerning the following question:

hi all

i am very new to perl so please forgive my stupidity

i have written the following program to verify some parameters of an interface of router .I have saved the file in same directory as the perl program(perl1.pl). when i do perl1.pl filename.txt i get the error that

Use of uninitialized value $second_file in open at C:\Users\Girish\Desktop\perl1 .pl line 10. No such file or directory at C:\Users\Girish\Desktop\perl1.pl line 10.

The program is:

use warnings; $second_file=$ARGV[0]; $counter=0; open FH_SECOND,">",$second_file or die $!; $line_second=<FH_SECOND>;

i solved the problem by using

open (FH_SECOND,"<",\$second_file) or die $!;

but can i use the declaration $line_second=<FH_SECOND>; to save the a line in the variable line_second

Replies are listed 'Best First'.
Re: File Handling problem
by charlesboyo (Beadle) on Aug 28, 2011 at 17:38 UTC
    Consider use strict; at the top of your code.

    my $second_file, my $counter etc are also good ideas.

    Try print $second_file just before the open statement. Does it actually contain a filename?

    Appears you're opening this file for output only. That's going to clobber whatever you got in there, so the next couple of <FH_SECOND> will have nothing to read as the file is opened for writing only - those lines will actually generate warnings.

    Is the file actually "writable"? (Though I'm quite sure that's not your intention). When you do fix your open statement, ensure that the file's permissions are appropriate.

      Thanks a lot for your help

      .

      i have modified it now and trying to resolve other problems in my code looking forward for more help

        Depending on the exact format of your text file you may be able to reduce the code to something like this ;
        #!perl use strict; open FH_SECOND,"<",$ARGV[0] or die $!; my @setting = ('Port Speed','Duplex Setting','Switchport Access Mode', +'Spanning Tree Mode'); my $line1 = <FH_SECOND>; if ($line1 =~ /description xxxxxxxxx/){ while (<FH_SECOND>){ my $msg = 'Gigabit '.shift @setting; if(/speed 1000|duplex full|switchport mode access|spanning-tree po +rtfast/){ print $msg." is OK!!!\n"; } else { print $msg." is WRONG!!!\n"; } } } else { print "The Configuration File $ARGV[0] is NOT the RIGHT FILE \n"; } close(FH_SECOND); # test file #description xxxxxxxxx #speed 1000 #duplex full #switchport mode access #spanning-tree portfast
        poj
Re: File Handling problem
by toolic (Bishop) on Aug 28, 2011 at 18:30 UTC
    i solved the problem by using open (FH_SECOND,"<",\$second_file) or die $!; but can i use the declaration $line_second=<FH_SECOND>; to save the a +line in the variable line_second
    Get rid of the backslash:
    open (FH_SECOND, "<", $second_file) or die $!;
    Now, line_second should contain the first line of your file, which you can confirm by printing it.
Re: File Handling problem
by ww (Archbishop) on Aug 28, 2011 at 19:33 UTC
    "when i do perl1.pl filename.txt i get the error ...."

    What exactly are you "do"ing... and what it your current working directory when you do it?

    Assuming from what you've provided that you're using some version of Windows, we still don't know for sure where and how you're executing the script:

    1. C:\Users\Girish\Desktop>perl1.pl filename.txt
    2. C:\>perl1.pl filename.txt
    3. C:\Documents and Settings\>perl perl1.pl filename.txt
    4. or, perhaps even from your file manager or somesuch?

    In that last case, $ARGV[0] will almost certainly be undef and might, therefore, account for something somewhat similar to the error message you've cited.

    The error you quote in your narrative appears to be inexact (or the script you posted is incomplete). There is no line 10 in the script you posted and unless you saved your script and the router file to the desktop (BAAAAD idea), the error message bears no obvious relationship to the rest of the information you've provided.

    IOW, we'll be better able to offer reasonable help if you tell us what's really in the code and what's really happening.

    As to what you did to solve " the problem by using"

    open (FH_SECOND,"<",\$second_file) or die $!;

    ...yes, that should solve at least one problem. The less_than sign tells your program to open $second_file for READING; your previous iteration (inside the code block, using a greater_than sign tells the script to open it for WRITING.

    Update: Fixed my errors in the pseudo-CLI list.