RFC on first perl script

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default RFC on first perl script



    Hi all
    well im trying at lerning this perl stuff.. reading from the "learning
    perl" oreilly book and a few other places,
    but also using perl a long time before i should ie making the below script,
    so that i dont get in to any very bad habbits at such an early stage.
    could some one please have a look at the code below and give comments, the
    code does do what i want it to ( well at leest from this stage).


    #!/usr/bin/perl -w
    ################################################## ##########
    # this function creates a arf rule file from an input file
    # Version 0.1 6/11/03
    ################################################## ##########
    @dataFile=<>; # read in file from command line
    @standardRules=`cat standard.for.arf.txt` ;

    foreach $site (@dataFile) { # loop for each line/site in dataFile
    chomp $site;

    ($siteLink,$siteNoOfPVCs,$siteAllPVCs)=split(/:/,$site,3); #split
    up main / pvc info
    ($siteIP,$siteString,$SiteName,$siteGroup,$siteCCT Reff,$siteACRate)=split(/,/,$siteLink,6);
    #split up main info
    (@sitePVCs)=split(/;/,$siteAllPVCs,$siteNoOfPVCs);

    my $siteARFfile = "$siteIP.arf";
    open(ARFfile, ">$siteARFfile") or die("can not open
    '$siteARFfile': $!");

    print ARFfile
    ("################################################ ######################
    \n# \n# Discover Rule for:
    $siteIP \n#
    \n################################################ ######################
    \n\n"); # print header

    print ARFfile ("@standardRules\n"); #print standard bits

    print ARFfile ("name matches \".*-Cpu-.*\": {\n \tsetName
    (\"$SiteName-RH-Cpu\") ;\n \tsetGroup (\"$siteGroup\")
    ;\n \tsetAlias (\"RH-Cpu\") ;\n} \n\n" ); # print -Cpu- rule

    print ARFfile ("name matches \".*-RH\": { \n \tsetName
    (\"$SiteName-RH\") ;\n \tsetGroup (\"$siteGroup\") ; \n \t
    setAlias (\"RH\") ;\n} \n\n" ); # print -RH rule

    print ARFfile ("name matches \".*RH-Serial.*\": {\n \tsetName
    (\"$SiteName-RH-WAN\$2\") ;\n \tsetGroup (\"$siteGr
    oup\") ;\n \tsetAlias (\"$siteCCTReff\") ;\n \tsetSpeedIn
    (\"$siteACRate\") ;\n \tsetSpeedOut (\"$siteACRate\") ;\n \tsetD
    eviceSpeedIn (\"$siteACRate\") ;\n \tsetDeviceSpeedOut (\"$siteACRate\")
    ;\n} \n\n"); # print RH-Serial rule


    print ARFfile ("name matches \".*-Serial.*\": {\n \tsetName
    (\"$SiteName-WAN\$2\") ;\n \tsetGroup (\"$siteGroup\"
    ) ;\n \tsetAlias (\"$siteCCTReff\") ;\n \tsetSpeedIn (\"$siteACRate\") ;\n
    \tsetSpeedOut (\"$siteACRate\") ;\n \tsetDevice
    SpeedIn (\"$siteACRate\") ;\n \tsetDeviceSpeedOut (\"$siteACRate\") ;\n}
    \n\n"); # print -Serial rule

    for ($i = 0; $i < $siteNoOfPVCs ; $i++ ) { # loop for each PVC

    ($PVCdlci,$PVCname,$PCVreff,$PVCcir)=split(/,/,"$sitePVCs[$i]",4);
    # split out pvc info

    print ARFfile ("name matches \".*-dlci-$PVCdlci\": {\n
    \tsetName (\"$SiteName-$PVCname\") ;\n \tsetGroup
    (\"$siteGroup\") ;\n \tsetAlias (\"$PCVreff\") ;\n \tsetSpeedIn
    (\"$PVCcir\") ;\n \tsetSpeedOut (\"$PVCcir\") ;\n \tsetDev
    iceSpeedIn (\"$PVCcir\") ;\n \tsetDeviceSpeedOut (\"$PVCcir\") ;\n}
    \n\n"); # print PVC rules


    }

    close(ARFfile) or die("can not close '$siteARFfile': $!");
    }



    ---
    fnord
    yes im a Concord Engineer, no it never flown!


    drowl@23.me.uk Guest

  2. Similar Questions and Discussions

    1. Control a non-perl image viewer from perl script
      Below is a (non-finished) script that trys to run a linux viewer called eog (eye of gnome) in a script that will eventually allow me to power thru...
    2. ASP --> PERL SCRIPT
      try using $ENV{THIS_SCRIPT}
    3. Help with Perl Script
      Any help on this would be great since its a free script there is no support. I have a website on a win2k server running MSFP 2002 extensions. The...
    4. ASP --> PERL SCRIPT HELP>>PLEASE>>
      Hello..I was browsing thru the newsgroups ...and was wondering if anyone here can be of assistance to me....as I am very very new to PERL..and have...
    5. Execute shell script from a perl script
      Hi, How can I executed a Unix shell script from a Perl script. The shell script is a dump of a oracle table to a file. The perl script is for...
  3. #2

    Default RE: RFC on first perl script

    > please have a look at the code below and give comments

    Here are some quick comments.

    #1. Always "use strict"
    #2. See #1.

    When you "use strict" it foeces you to do things the "right way" and will
    help catch errors because of the extra checks it makes.

    So something like this:
    > @dataFile=<>; # read in file from command line
    Needs to be changed to this by explicitly declaring that variable:
    my @dataFile=<>; # read in file from command line
    > @standardRules=`cat standard.for.arf.txt` ;
    This isn't portable (if you care for it to be), and does not check for
    errors. This might be better:

    open IN, 'standard.for.arf.txt' or die $!;
    my @standardRules = <IN>;
    close IN;
    > (@sitePVCs)=split(/;/,$siteAllPVCs,$siteNoOfPVCs);
    The "(" and ")" force list context. The array @sitePVCs will already force
    list context without the parens. This can be rewriten like this, which may
    or may not be more readable to you:

    my @sitePVCs = split(/;/,$siteAllPVCs,$siteNoOfPVCs);
    > open(ARFfile, ">$siteARFfile") or die("can not open
    Typically filehandles are in all caps. They don't need to be, but it is the
    usual way of doing things because it makes them easier to spot (especially
    to people other than the author). Also the parens are not needed because
    "or" has very low precedence. I also tend to put my error condition on the
    next line, but that is just my preference.

    open ARFFILE, ">$siteARFfile"
    or die "can not open '$siteARFfile': $!";

    Again, parens not needed here, but they don't hurt either:
    > print ARFfile ("@standardRules\n"); #print standard bits
    print ARFFILE "@standardRules\n"; #print standard bits

    This is pretty icky:
    > print ARFfile ("name matches \".*RH-Serial.*\":
    > {\n \tsetName(\"$SiteName-RH-WAN\$2\") ;\n \tsetGroup
    > (\"$siteGroup\") ..<snip>.."); # print RH-Serial rule
    Try a here-document instead:

    # print RH-Serial rule
    print ARFFILE <<EOF;
    name matches ".*RH-Serial.*": {
    setName("$SiteName-RH-WAN\$2");
    setGroup("$siteGroup");
    setAlias("$siteCCTReff");
    setSpeedIn("$siteACRate");
    setSpeedOut("$siteACRate");
    setDeviceSpeedIn("$siteACRate");
    setDeviceSpeedOut("$siteACRate");
    }
    EOF

    It makes it a lot easier to read, not to mention I could remove the \n and
    the \" escapes. BTW - If you have quotes in your string you can do this
    qq[a "blah" b] instead of "a \"blah\" b". The char following the "qq" can
    be any char, so you could use qq{}, qq||, qq**, etc.

    In general there isn't anything *wrong* with the script... but "use strict"
    is STRONGLY encouraged. The rest are just suggestions for readability.

    Rob

    -----Original Message-----
    From: [email]drowl@23.me.uk[/email] [mailto:drowl@23.me.uk]
    Sent: Thursday, November 06, 2003 11:34 AM
    To: [email]beginners@perl.org[/email]
    Subject: RFC on first perl script




    Hi all
    well im trying at lerning this perl stuff.. reading from the "learning
    perl" oreilly book and a few other places,
    but also using perl a long time before i should ie making the below script,
    so that i dont get in to any very bad habbits at such an early stage.
    could some one please have a look at the code below and give comments, the
    code does do what i want it to ( well at leest from this stage).


    #!/usr/bin/perl -w
    ################################################## ##########
    # this function creates a arf rule file from an input file
    # Version 0.1 6/11/03
    ################################################## ##########
    @dataFile=<>; # read in file from command line
    @standardRules=`cat standard.for.arf.txt` ;

    foreach $site (@dataFile) { # loop for each line/site in dataFile
    chomp $site;

    ($siteLink,$siteNoOfPVCs,$siteAllPVCs)=split(/:/,$site,3); #split
    up main / pvc info

    ($siteIP,$siteString,$SiteName,$siteGroup,$siteCCT Reff,$siteACRate)=split(/,
    /,$siteLink,6);
    #split up main info
    (@sitePVCs)=split(/;/,$siteAllPVCs,$siteNoOfPVCs);

    my $siteARFfile = "$siteIP.arf";
    open(ARFfile, ">$siteARFfile") or die("can not open
    '$siteARFfile': $!");

    print ARFfile
    ("################################################ ######################
    \n# \n# Discover Rule for:
    $siteIP \n#
    \n################################################ ######################
    \n\n"); # print header

    print ARFfile ("@standardRules\n"); #print standard bits

    print ARFfile ("name matches \".*-Cpu-.*\": {\n \tsetName
    (\"$SiteName-RH-Cpu\") ;\n \tsetGroup (\"$siteGroup\")
    ;\n \tsetAlias (\"RH-Cpu\") ;\n} \n\n" ); # print -Cpu- rule

    print ARFfile ("name matches \".*-RH\": { \n \tsetName
    (\"$SiteName-RH\") ;\n \tsetGroup (\"$siteGroup\") ; \n \t
    setAlias (\"RH\") ;\n} \n\n" ); # print -RH rule

    print ARFfile ("name matches \".*RH-Serial.*\": {\n \tsetName
    (\"$SiteName-RH-WAN\$2\") ;\n \tsetGroup (\"$siteGr
    oup\") ;\n \tsetAlias (\"$siteCCTReff\") ;\n \tsetSpeedIn
    (\"$siteACRate\") ;\n \tsetSpeedOut (\"$siteACRate\") ;\n \tsetD
    eviceSpeedIn (\"$siteACRate\") ;\n \tsetDeviceSpeedOut (\"$siteACRate\")
    ;\n} \n\n"); # print RH-Serial rule


    print ARFfile ("name matches \".*-Serial.*\": {\n \tsetName
    (\"$SiteName-WAN\$2\") ;\n \tsetGroup (\"$siteGroup\"
    ) ;\n \tsetAlias (\"$siteCCTReff\") ;\n \tsetSpeedIn (\"$siteACRate\") ;\n
    \tsetSpeedOut (\"$siteACRate\") ;\n \tsetDevice
    SpeedIn (\"$siteACRate\") ;\n \tsetDeviceSpeedOut (\"$siteACRate\") ;\n}
    \n\n"); # print -Serial rule

    for ($i = 0; $i < $siteNoOfPVCs ; $i++ ) { # loop for each PVC


    ($PVCdlci,$PVCname,$PCVreff,$PVCcir)=split(/,/,"$sitePVCs[$i]",4);
    # split out pvc info

    print ARFfile ("name matches \".*-dlci-$PVCdlci\": {\n
    \tsetName (\"$SiteName-$PVCname\") ;\n \tsetGroup
    (\"$siteGroup\") ;\n \tsetAlias (\"$PCVreff\") ;\n \tsetSpeedIn
    (\"$PVCcir\") ;\n \tsetSpeedOut (\"$PVCcir\") ;\n \tsetDev
    iceSpeedIn (\"$PVCcir\") ;\n \tsetDeviceSpeedOut (\"$PVCcir\") ;\n}
    \n\n"); # print PVC rules


    }

    close(ARFfile) or die("can not close '$siteARFfile': $!");
    }



    ---
    fnord
    yes im a Concord Engineer, no it never flown!



    --
    To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    Rob Hanson Guest

  4. #3

    Default Re: RFC on first perl script

    On Thu, 06 Nov 2003 16:33:41 +0000, drowl wrote:
    > #!/usr/bin/perl -w
    No big deal, but - IMO - easier to read, and it adds strict;

    #!/usr/bin/perl
    #
    use strict;
    use warnings;
    > @dataFile=<>; # read in file from command line
    > @standardRules=`cat standard.for.arf.txt` ;
    my @dataFile = <>;
    my @standardRules = `cat standard.for.arf.txt`;

    Also have in mind that this is platform dependent, as there is no 'cat'
    command in DOS/Windows (or on many other platforms, I would guess).

    Instead of doing the whole work with open, read and close all the time,
    you could do as me: Write your own module which has a 'read_file'
    function;

    sub read_file {
    my $filename = shift || '';

    my @lines = ();
    if ( $filename && -e $filename ) {
    if ( open(FILE, $filename) ) {
    @lines = <FILE>;
    close( FILE );
    chomp( @lines );
    }
    }

    return ( wantarray ) ? @lines : join("\n", @lines);
    }

    This one is very simplified, but it gives you and idea. Next time you
    need to read a (text) file:

    my $text = read_file( 'text.txt' );
    > #split up main / pvc info
    > ($siteLink,$siteNoOfPVCs,$siteAllPVCs)=split(/:/,$site,3);
    As long as we don't know what the contents of $site looks like, we can't
    comment on this.
    > for ($i = 0; $i < $siteNoOfPVCs ; $i++ ) { # loop for each PVC
    I guess this should do the trick:

    foreach ( @sitePVCs ) {
    # ...
    }


    --
    Tore Aursand <tore@aursand.no>

    Tore Aursand Guest

  5. #4

    Default RE: RFC on first perl script


    Good stuff all taken on board
    did take me a while to figger out that EOF had to be at the begging of
    the line tho, but i got there in the end...

    and a question about "use strict"

    i now get the below warning along with many others...
    how does one declair a varible then?

    Global symbol "$site" requires explicit package name at ./makeArf.pl line 17.



    thank you
    Ritch
    >> please have a look at the code below and give comments
    >
    > Here are some quick comments.
    >
    > #1. Always "use strict"
    > #2. See #1.
    >
    > When you "use strict" it foeces you to do things the "right way" and
    > will help catch errors because of the extra checks it makes.
    >
    > So something like this:
    >> @dataFile=<>; # read in file from command line
    >
    > Needs to be changed to this by explicitly declaring that variable: my
    > @dataFile=<>; # read in file from command line
    >
    >> @standardRules=`cat standard.for.arf.txt` ;
    >
    > This isn't portable (if you care for it to be), and does not check for
    > errors. This might be better:
    >
    > open IN, 'standard.for.arf.txt' or die $!;
    > my @standardRules = <IN>;
    > close IN;
    >
    >> (@sitePVCs)=split(/;/,$siteAllPVCs,$siteNoOfPVCs);
    >
    > The "(" and ")" force list context. The array @sitePVCs will already
    > force list context without the parens. This can be rewriten like this,
    > which may or may not be more readable to you:
    >
    > my @sitePVCs = split(/;/,$siteAllPVCs,$siteNoOfPVCs);
    >
    >> open(ARFfile, ">$siteARFfile") or die("can not open
    >
    > Typically filehandles are in all caps. They don't need to be, but it is
    > the usual way of doing things because it makes them easier to spot
    > (especially to people other than the author). Also the parens are not
    > needed because "or" has very low precedence. I also tend to put my
    > error condition on the next line, but that is just my preference.
    >
    > open ARFFILE, ">$siteARFfile"
    > or die "can not open '$siteARFfile': $!";
    >
    > Again, parens not needed here, but they don't hurt either:
    >
    >> print ARFfile ("@standardRules\n"); #print standard bits
    > print ARFFILE "@standardRules\n"; #print standard bits
    >
    > This is pretty icky:
    >> print ARFfile ("name matches \".*RH-Serial.*\":
    >> {\n \tsetName(\"$SiteName-RH-WAN\$2\") ;\n \tsetGroup
    >> (\"$siteGroup\") ..<snip>.."); # print RH-Serial rule
    >
    > Try a here-document instead:
    >
    > # print RH-Serial rule
    > print ARFFILE <<EOF;
    > name matches ".*RH-Serial.*": {
    > setName("$SiteName-RH-WAN\$2");
    > setGroup("$siteGroup");
    > setAlias("$siteCCTReff");
    > setSpeedIn("$siteACRate");
    > setSpeedOut("$siteACRate");
    > setDeviceSpeedIn("$siteACRate");
    > setDeviceSpeedOut("$siteACRate");
    > }
    > EOF
    >
    > It makes it a lot easier to read, not to mention I could remove the \n
    > and the \" escapes. BTW - If you have quotes in your string you can do
    > this qq[a "blah" b] instead of "a \"blah\" b". The char following the
    > "qq" can be any char, so you could use qq{}, qq||, qq**, etc.
    >
    > In general there isn't anything *wrong* with the script... but "use
    > strict" is STRONGLY encouraged. The rest are just suggestions for
    > readability.
    >
    > Rob
    >
    > -----Original Message-----
    > From: [email]drowl@23.me.uk[/email] [mailto:drowl@23.me.uk]
    > Sent: Thursday, November 06, 2003 11:34 AM
    > To: [email]beginners@perl.org[/email]
    > Subject: RFC on first perl script
    >
    >
    >
    >
    > Hi all
    > well im trying at lerning this perl stuff.. reading from the "learning
    > perl" oreilly book and a few other places,
    > but also using perl a long time before i should ie making the below
    > script, so that i dont get in to any very bad habbits at such an early
    > stage. could some one please have a look at the code below and give
    > comments, the code does do what i want it to ( well at leest from this
    > stage).
    >
    >
    > #!/usr/bin/perl -w
    > ################################################## ##########
    > # this function creates a arf rule file from an input file
    > # Version 0.1 6/11/03
    > ################################################## ##########
    > @dataFile=<>; # read in file from command line
    > @standardRules=`cat standard.for.arf.txt` ;
    >
    > foreach $site (@dataFile) { # loop for each line/site in dataFile
    > chomp $site;
    >
    > ($siteLink,$siteNoOfPVCs,$siteAllPVCs)=split(/:/,$site,3);
    > #split
    > up main / pvc info
    >
    > ($siteIP,$siteString,$SiteName,$siteGroup,$siteCCT Reff,$siteACRate)=split(/,
    > /,$siteLink,6);
    > #split up main info
    > (@sitePVCs)=split(/;/,$siteAllPVCs,$siteNoOfPVCs);
    >
    > my $siteARFfile = "$siteIP.arf";
    > open(ARFfile, ">$siteARFfile") or die("can not open
    > '$siteARFfile': $!");
    >
    > print ARFfile
    > ("################################################ ######################
    > \n# \n# Discover Rule for:
    > $siteIP \n#
    > \n################################################ ######################
    > \n\n"); # print header
    >
    > print ARFfile ("@standardRules\n"); #print standard bits
    >
    > print ARFfile ("name matches \".*-Cpu-.*\": {\n \tsetName
    > (\"$SiteName-RH-Cpu\") ;\n \tsetGroup (\"$siteGroup\")
    > ;\n \tsetAlias (\"RH-Cpu\") ;\n} \n\n" ); # print -Cpu- rule
    >
    > print ARFfile ("name matches \".*-RH\": { \n \tsetName
    > (\"$SiteName-RH\") ;\n \tsetGroup (\"$siteGroup\") ; \n \t
    > setAlias (\"RH\") ;\n} \n\n" ); # print -RH rule
    >
    > print ARFfile ("name matches \".*RH-Serial.*\": {\n \tsetName
    > (\"$SiteName-RH-WAN\$2\") ;\n \tsetGroup (\"$siteGr
    > oup\") ;\n \tsetAlias (\"$siteCCTReff\") ;\n \tsetSpeedIn
    > (\"$siteACRate\") ;\n \tsetSpeedOut (\"$siteACRate\") ;\n \tsetD
    > eviceSpeedIn (\"$siteACRate\") ;\n \tsetDeviceSpeedOut (\"$siteACRate\")
    > ;\n} \n\n"); # print RH-Serial rule
    >
    >
    > print ARFfile ("name matches \".*-Serial.*\": {\n \tsetName
    > (\"$SiteName-WAN\$2\") ;\n \tsetGroup (\"$siteGroup\"
    > ) ;\n \tsetAlias (\"$siteCCTReff\") ;\n \tsetSpeedIn (\"$siteACRate\")
    > ;\n \tsetSpeedOut (\"$siteACRate\") ;\n \tsetDevice
    > SpeedIn (\"$siteACRate\") ;\n \tsetDeviceSpeedOut (\"$siteACRate\") ;\n}
    > \n\n"); # print -Serial rule
    >
    > for ($i = 0; $i < $siteNoOfPVCs ; $i++ ) { # loop for each PVC
    >
    >
    > ($PVCdlci,$PVCname,$PCVreff,$PVCcir)=split(/,/,"$sitePVCs[$i]",4); #
    > split out pvc info
    >
    > print ARFfile ("name matches \".*-dlci-$PVCdlci\": {\n
    > \tsetName (\"$SiteName-$PVCname\") ;\n \tsetGroup
    > (\"$siteGroup\") ;\n \tsetAlias (\"$PCVreff\") ;\n \tsetSpeedIn
    > (\"$PVCcir\") ;\n \tsetSpeedOut (\"$PVCcir\") ;\n \tsetDev
    > iceSpeedIn (\"$PVCcir\") ;\n \tsetDeviceSpeedOut (\"$PVCcir\") ;\n}
    > \n\n"); # print PVC rules
    >
    >
    > }
    >
    > close(ARFfile) or die("can not close '$siteARFfile': $!");
    > }
    >
    >
    >
    > ---
    > fnord
    > yes im a Concord Engineer, no it never flown!
    >
    >
    >
    > --
    > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    >
    > --
    > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > For additional commands, e-mail: [email]beginners-help@perl.org[/email]

    --



    drowl@23.me.uk Guest

  6. #5

    Default Re: RFC on first perl script

    > On Thu, 06 Nov 2003 16:33:41 +0000, drowl wrote:
    >> #!/usr/bin/perl -w
    >
    > No big deal, but - IMO - easier to read, and it adds strict;
    >
    > #!/usr/bin/perl
    > #
    > use strict;
    > use warnings;
    >
    >> @dataFile=<>; # read in file from command line
    >> @standardRules=`cat standard.for.arf.txt` ;
    >
    > my @dataFile = <>;
    > my @standardRules = `cat standard.for.arf.txt`;
    >
    > Also have in mind that this is platform dependent, as there is no 'cat'
    > command in DOS/Windows (or on many other platforms, I would guess).
    >
    > Instead of doing the whole work with open, read and close all the time,
    > you could do as me: Write your own module which has a 'read_file'
    > function;
    >
    > sub read_file {
    > my $filename = shift || '';
    >
    > my @lines = ();
    > if ( $filename && -e $filename ) {
    > if ( open(FILE, $filename) ) {
    > @lines = <FILE>;
    > close( FILE );
    > chomp( @lines );
    > }
    > }
    >
    > return ( wantarray ) ? @lines : join("\n", @lines);
    > }
    >
    > This one is very simplified, but it gives you and idea. Next time you
    > need to read a (text) file:
    >
    > my $text = read_file( 'text.txt' );
    >
    nice... how ever i hope to turn this into a sub with $site as input
    and $siteIP and $siteString as output + the arf file of course

    but maybe i can use this in the main proggi..
    >> #split up main / pvc info
    >> ($siteLink,$siteNoOfPVCs,$siteAllPVCs)=split(/:/,$site,3);
    >
    > As long as we don't know what the contents of $site looks like, we can't
    > comment on this.
    $site would look like:
    127.0.0.1,comunityString,sitename,group,e23,20000: 2:bsite,21,p235,32000;csite,22,p523,64000
    >
    >> for ($i = 0; $i < $siteNoOfPVCs ; $i++ ) { # loop for each PVC
    >
    > I guess this should do the trick:
    >
    > foreach ( @sitePVCs ) {
    > # ...
    > }
    >
    >
    humm then would i just use ...=split(/,/,$_,4); ???

    > --
    > Tore Aursand <tore@aursand.no>
    >
    >
    >

    Thanks
    Ritch
    --
    fnord
    yes im a Concord Engineer, no it never flown!


    drowl@23.me.uk Guest

  7. #6

    Default Re: RFC on first perl script


    On Thursday, Nov 6, 2003, at 09:56 US/Pacific, [email]drowl@23.me.uk[/email] wrote:
    > i now get the below warning along with many others...
    > how does one declair a varible then?
    >
    > Global symbol "$site" requires explicit package name at ./makeArf.pl
    > line 17.

    I think your hit is at:

    foreach $site (@dataFile) { # loop

    and that should have been

    foreach my $site (@dataFile) { # loop

    this way '$site' is limited to the scope of the foreach loop.


    ciao
    drieux

    ---

    Drieux Guest

  8. #7

    Default RE: RFC on first perl script

    > Global symbol "$site" requires explicit package name at ./makeArf.pl line 17.

    One of the things about strict is it makes you declare the scope of your
    variables before using them. So, for instance, while:

    #! /usr/bin/perl
    $foo = "foo\n";
    print $foo;

    Would run, the following wouldn't:

    #! /usr/bin/perl
    use warnings; # yelp and whine if we screw up
    use strict; # force us to not be sloppy.
    $foo = "foo\n";
    print $foo;

    It would cause perl to say:
    Global symbol "$foo" requires explicit package name at - line 4

    We could fix that by changing like 4 to one of the following:
    my $foo = "foo\n";
    our $foo = "foo\n";
    local $foo = "foo\n";
    >From Perldoc:
    my EXPR
    my TYPE EXPR
    my EXPR : ATTRS
    my TYPE EXPR : ATTRS

    A "my" declares the listed variables to be local (lexically)
    to the enclosing block, file, or "eval". If more than one value is
    listed, the list must be placed in parentheses.

    The exact semantics and interface of TYPE and ATTRS are
    still evolving. TYPE is currently bound to the use of "fields" pragma,
    and attributes are handled using the "attributes" pragma, or starting
    from Perl 5.8.0 also via the "Attribute::Handlers" module. See "Private
    Variables via my()" in perlsub for details, and fields, attributes, and
    Attribute::Handlers.

    local EXPR

    You really probably want to be using "my" instead, because
    "local" isn't what most people think of as "local". See
    "Private Variables via my()" in perlsub for details.

    A local modifies the listed variables to be local to the
    enclosing block, file, or eval. If more than one value is
    listed, the list must be placed in parentheses. See "Temporary our
    EXPR
    our EXPR TYPE
    our EXPR : ATTRS
    our TYPE EXPR : ATTRS

    An "our" declares the listed variables to be valid globals
    within the enclosing block, file, or "eval". That is, it has the
    same scoping rules as a "my" declaration, but does not create a
    local variable. If more than one value is listed, the list must
    be placed in parentheses. The "our" declaration has no semantic
    effect unless "use strict vars" is in effect, in which case it
    lets you use the declared global variable without qualifying it
    with a package name. (But only within the lexical scope of the
    "our" declaration. In this it differs from "use vars", which is
    package scoped.)

    An "our" declaration declares a global variable that will be
    visible across its entire lexical scope, even across package
    boundaries. The package in which the variable is entered is
    determined at the point of the declaration, not at the point of
    use. This means the following behavior holds:

    package Foo;
    our $bar; # declares $Foo::bar for rest of lexical scope
    $bar = 20;

    package Bar;
    print $bar; # prints 20

    Multiple "our" declarations in the same lexical scope are
    allowed if they are in different packages. If they happened to
    be in the same package, Perl will emit warnings if you have
    asked for them.

    use warnings;
    package Foo;
    our $bar; # declares $Foo::bar for rest of lexical scope
    $bar = 20;

    package Bar;
    our $bar = 30; # declares $Bar::bar for rest of lexical scope
    print $bar; # prints 30

    our $bar; # emits warning

    An "our" declaration may also have a list of attributes
    associated with it.

    The exact semantics and interface of TYPE and ATTRS are still
    evolving. TYPE is currently bound to the use of "fields" pragma,
    and attributes are handled using the "attributes" pragma, or
    starting from Perl 5.8.0 also via the "Attribute::Handlers"
    module. See "Private Variables via my()" in perlsub for details,
    and fields, attributes, and Attribute::Handlers.

    The only currently recognized "our()" attribute is "unique"
    which indicates that a single copy of the global is to be used
    by all interpreters should the program happen to be running in a
    multi-interpreter environment. (The default behaviour would be
    for each interpreter to have its own copy of the global.)
    Examples:

    our @EXPORT : unique = qw(foo);
    our %EXPORT_TAGS : unique = (bar => [qw(aa bb cc)]);
    our $VERSION : unique = "1.00";

    Note that this attribute also has the effect of making the
    global readonly when the first new interpreter is cloned (for
    example, when the first new thread is created).

    Multi-interpreter environments can come to being either through
    the fork() emulation on Windows platforms, or by embedding perl
    in a multi-threaded application. The "unique" attribute does
    nothing in all other environments.

    Values via local()" in perlsub for details, including issues
    with tied arrays and hashes.

    -Dan
    Dan Anderson Guest

  9. #8

    Default RE: RFC on first perl script

    Howdy,

    Always use strict;
    Then your variables won't get messy, see the perldoc strict for more details.
    > foreach $site (@dataFile) { # loop for each line/site in dataFile
    > chomp $site;
    You might make your life easier to by not declaring a variable at all here:

    for(@datafile) {
    chomp;
    ....

    Then you just use $_ where you would have used $site and you're all set.
    (Except with functions that are expecting $_ if nothing else is specified,
    like chomp for instance.)

    But yes to declare a variable with use strcit you need to do my before and
    that will elt you use it within the block you declared it in.

    HTH

    DMuey
    >
    >
    > ($siteLink,$siteNoOfPVCs,$siteAllPVCs)=split(/:/,$site,3);
    > #split up main / pvc info
    >
    > ($siteIP,$siteString,$SiteName,$siteGroup,$siteCCT Reff,$siteAC
    > Rate)=split(/,/,$siteLink,6);
    > #split up main info
    > (@sitePVCs)=split(/;/,$siteAllPVCs,$siteNoOfPVCs);
    >
    > my $siteARFfile = "$siteIP.arf";
    > open(ARFfile, ">$siteARFfile") or die("can not open
    > '$siteARFfile': $!");
    >
    > print ARFfile
    > ("################################################ ############
    > ##########
    > \n# \n# Discover Rule for:
    > $siteIP \n#
    > \n################################################ ############
    > ##########
    > \n\n"); # print header
    >
    > print ARFfile ("@standardRules\n"); #print standard bits
    >
    > print ARFfile ("name matches \".*-Cpu-.*\": {\n \tsetName
    > (\"$SiteName-RH-Cpu\") ;\n \tsetGroup (\"$siteGroup\")
    > ;\n \tsetAlias (\"RH-Cpu\") ;\n} \n\n" ); # print -Cpu- rule
    >
    > print ARFfile ("name matches \".*-RH\": { \n \tsetName
    > (\"$SiteName-RH\") ;\n \tsetGroup (\"$siteGroup\") ; \n \t
    > setAlias (\"RH\") ;\n} \n\n" ); # print -RH rule
    >
    > print ARFfile ("name matches \".*RH-Serial.*\": {\n \tsetName
    > (\"$SiteName-RH-WAN\$2\") ;\n \tsetGroup (\"$siteGr
    > oup\") ;\n \tsetAlias (\"$siteCCTReff\") ;\n \tsetSpeedIn
    > (\"$siteACRate\") ;\n \tsetSpeedOut (\"$siteACRate\") ;\n
    > \tsetD eviceSpeedIn (\"$siteACRate\") ;\n \tsetDeviceSpeedOut
    > (\"$siteACRate\") ;\n} \n\n"); # print RH-Serial rule
    >
    >
    > print ARFfile ("name matches \".*-Serial.*\": {\n \tsetName
    > (\"$SiteName-WAN\$2\") ;\n \tsetGroup (\"$siteGroup\"
    > ) ;\n \tsetAlias (\"$siteCCTReff\") ;\n \tsetSpeedIn
    > (\"$siteACRate\") ;\n \tsetSpeedOut (\"$siteACRate\") ;\n
    > \tsetDevice SpeedIn (\"$siteACRate\") ;\n \tsetDeviceSpeedOut
    > (\"$siteACRate\") ;\n} \n\n"); # print -Serial rule
    >
    > for ($i = 0; $i < $siteNoOfPVCs ; $i++ ) { # loop for each PVC
    >
    >
    > ($PVCdlci,$PVCname,$PCVreff,$PVCcir)=split(/,/,"$sitePVCs[$i]",4);
    > # split out pvc info
    >
    > print ARFfile ("name matches
    > \".*-dlci-$PVCdlci\": {\n \tsetName (\"$SiteName-$PVCname\")
    > ;\n \tsetGroup
    > (\"$siteGroup\") ;\n \tsetAlias (\"$PCVreff\") ;\n \tsetSpeedIn
    > (\"$PVCcir\") ;\n \tsetSpeedOut (\"$PVCcir\") ;\n \tsetDev
    > iceSpeedIn (\"$PVCcir\") ;\n \tsetDeviceSpeedOut
    > (\"$PVCcir\") ;\n} \n\n"); # print PVC rules
    >
    >
    > }
    >
    > close(ARFfile) or die("can not close '$siteARFfile': $!"); }
    >
    >
    >
    > ---
    > fnord
    > yes im a Concord Engineer, no it never flown!
    >
    >
    >
    > --
    > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    >
    >
    Dan Muey Guest

  10. #9

    Default Re: RFC on first perl script

    [email]drowl@23.me.uk[/email] wrote:
    > Hi all
    > well im trying at lerning this perl stuff.. reading from the "learning
    > perl" oreilly book and a few other places,
    > but also using perl a long time before i should ie making the below script,
    > so that i dont get in to any very bad habbits at such an early stage.
    > could some one please have a look at the code below and give comments, the
    > code does do what i want it to ( well at leest from this stage).
    I would comment on the process here. I am sure that you know what you want, but do we? Since you say
    it does what you want, I assume it compiles. While it may not be impossible to write good code by just
    jumping into the coding, it is highly unlikely to happen.
    > #!/usr/bin/perl -w
    > ################################################## ##########
    > # this function creates a arf rule file from an input file
    > # Version 0.1 6/11/03
    > ################################################## ##########
    use strict;
    use warnings;
    > [snip]
    Now recompile your code with those two lines at the top, and then repost, referably telling us, in
    real-world terms, what problem you are trying to solve, or what task you are trying to accomplish, with
    your program. Defining your objective is the first step in solid programming.

    Joseph


    R. Joseph Newton Guest

  11. #10

    Default Re: RFC on first perl script

    <drowl@23.me.uk> wrote:
    >
    > well im trying at lerning this perl stuff.. reading from the "learning
    > perl" oreilly book and a few other places,
    > but also using perl a long time before i should ie making the below script,
    > so that i dont get in to any very bad habbits at such an early stage.
    > could some one please have a look at the code below and give comments, the
    > code does do what i want it to ( well at leest from this stage).
    [snip code]

    You seem to lack care. All lower-case and misspellings are likely
    to be reflected in your programming.
    > fnord
    > yes im a Concord Engineer, no it never flown!
    The aeroplane is spelled 'Concorde'. If you were truly on
    the engineering team for her then I have a million
    questions for you!

    Rob


    Rob Dixon Guest

  12. #11

    Default Re: RFC on first perl script

    <drowl@23.me.uk> wrote:
    >
    > well im trying at lerning this perl stuff.. reading from the "learning
    > perl" oreilly book and a few other places,
    > but also using perl a long time before i should ie making the below script,
    > so that i dont get in to any very bad habbits at such an early stage.
    > could some one please have a look at the code below and give comments, the
    > code does do what i want it to ( well at leest from this stage).
    [snip code]

    Please post again, with the guidelines that others in this thread
    have set.

    If you can explain your problem properly then you have more
    or less written your code.

    << The best software describes the data, just as the best
    massage describes the person. >>

    Rob


    Rob Dixon Guest

  13. #12

    Default Re: RFC on first perl script

    > <drowl@23.me.uk> wrote:
    >>
    >> well im trying at lerning this perl stuff.. reading from the
    >> "learning
    >> perl" oreilly book and a few other places,
    >> but also using perl a long time before i should ie making the below
    >> script, so that i dont get in to any very bad habbits at such an early
    >> stage. could some one please have a look at the code below and give
    >> comments, the code does do what i want it to ( well at leest from this
    >> stage).
    >
    > [snip code]
    >
    > You seem to lack care. All lower-case and misspellings are likely
    > to be reflected in your programming.
    >
    are yes sorry im dyslexic and lazzy, luckly when i spell a command wrong,
    perl lets me know...
    >> fnord
    >> yes im a Concord Engineer, no it never flown!
    >
    > The aeroplane is spelled 'Concorde'. If you were truly on
    > the engineering team for her then I have a million
    > questions for you!
    >
    > Rob
    >
    >
    yes i am not a 'Concorde' engineer im a Concord engineer
    as you pointed out 'Concorde' is an aeroplane,
    'Concord' is a network managment package ie [url]http://www.concord.com[/url]

    (i dont work for them i work for a companny the uses it, but my job title
    is Concord engineer)
    >
    > --
    > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > For additional commands, e-mail: [email]beginners-help@perl.org[/email]

    --



    drowl@23.me.uk Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139