Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] strict-izing my code

Expand Messages
  • Jenda Krynicky
    From: devin71091 ... You don t need this variable. ... You would have to store it the other way around. The key of a hash must be a
    Message 1 of 3 , Mar 26, 2004
    • 0 Attachment
      From: "devin71091" <mark.devine@...>
      > The following script works if I comment out the use strict line but
      > when I run it upcommented I get the following error: Can't use string
      > ("*SNMPHANDLE9") as a symbol ref while "strict refs" in use at
      > ./multiplefilehandler.pl line 20.
      >
      > #!/usr/bin/perl
      >
      > use strict;
      >
      > my $clear = `/usr/bin/clear`;
      > print $clear;
      >
      > my $filehandle = "SNMPHANDLE";

      You don't need this variable.

      > my (%hasharray, @ARRAY, @ARRAY1, $element);
      >
      > # key is filehandle, value is file

      You would have to store it the other way around.
      The key of a hash must be a string, the value may be something else.
      For example a filehandle (IO::File) object.

      In this case I do not see any reason to store it at all.

      > @ARRAY = `ls /tmp/myfile*`;

      Better would be
      @ARRAY = glob '/tmp/myfile*';

      > for (my $i = 0; $i <= $#ARRAY; $i++) {
      > $hasharray{"*$filehandle$i"}=$ARRAY[$i];
      > }

      If you did need to keep thosd several filehandles open you could do
      it like this:

      foreach my $file (@ARRAY) {
      my $FH;
      open $FH, '+<', "$file"
      or die "Can't open $file: $!\n";
      $hasharray{$file} = $FH;
      }


      > my $k;
      > foreach $k (keys(%hasharray)) {

      In a recent perl it may be written as

      foreach my $k (keys(%hasharray)) {

      > chomp $hasharray{$k};

      Thanks to using glob() instead of `` you don't need this

      > open($k, "+< $hasharray{$k}");
      > @ARRAY1 = <$k>;
      > foreach $element (@ARRAY1) {
      > if ($element=~/^(.*Access.*)$/) {
      > print "$hasharray{$k} : $element\n";
      > }
      > }
      > seek($k,0,0);
      > print $k @ARRAY1;
      > truncate($k,tell($k));
      > close($k);
      > }
      >
      > Without massively abusing me for my code can you tell me what I have
      > to do to be able to run this with strict refs?

      Since you do not keep several files opened at once you do not need
      the hash and you do not need to use several different filehandles.
      You can replace both loops by:

      foreach my $file (@ARRAY) {
      open(my $FH, "+< $file");
      my @ARRAY1 = <$FH>;
      foreach my $element (@ARRAY1) {
      if ($element=~/^(.*Access.*)$/) {
      print "$file : $element\n";
      }
      }
      seek($FH,0,0);
      print $FH @ARRAY1;
      truncate($FH,tell($FH));
      close($FH);
      }


      Please do consider using better variable names!
      @ARRAY and @ARRAY1 are not very descriptive.
      @files and @elements would be much better.

      HTH, Jenda
      ===== Jenda@... === http://Jenda.Krynicky.cz =====
      When it comes to wine, women and song, wizards are allowed
      to get drunk and croon as much as they like.
      -- Terry Pratchett in Sourcery
    • Mark Devine
      thank you
      Message 2 of 3 , Mar 29, 2004
      • 0 Attachment
        thank you

        Jenda Krynicky wrote:
        > From: "devin71091" <mark.devine@...>
        > > The following script works if I comment out the use strict line but
        > > when I run it upcommented I get the following error: Can't use string
        > > ("*SNMPHANDLE9") as a symbol ref while "strict refs" in use at
        > > ./multiplefilehandler.pl line 20.
        > >
        > > #!/usr/bin/perl
        > >
        > > use strict;
        > >
        > > my $clear = `/usr/bin/clear`;
        > > print $clear;
        > >
        > > my $filehandle = "SNMPHANDLE";
        >
        > You don't need this variable.
        >
        > > my (%hasharray, @ARRAY, @ARRAY1, $element);
        > >
        > > # key is filehandle, value is file
        >
        > You would have to store it the other way around.
        > The key of a hash must be a string, the value may be something else.
        > For example a filehandle (IO::File) object.
        >
        > In this case I do not see any reason to store it at all.
        >
        > > @ARRAY = `ls /tmp/myfile*`;
        >
        > Better would be
        > @ARRAY = glob '/tmp/myfile*';
        >
        > > for (my $i = 0; $i <= $#ARRAY; $i++) {
        > > $hasharray{"*$filehandle$i"}=$ARRAY[$i];
        > > }
        >
        > If you did need to keep thosd several filehandles open you could do
        > it like this:
        >
        > foreach my $file (@ARRAY) {
        > my $FH;
        > open $FH, '+<', "$file"
        > or die "Can't open $file: $!\n";
        > $hasharray{$file} = $FH;
        > }
        >
        >
        > > my $k;
        > > foreach $k (keys(%hasharray)) {
        >
        > In a recent perl it may be written as
        >
        > foreach my $k (keys(%hasharray)) {
        >
        > > chomp $hasharray{$k};
        >
        > Thanks to using glob() instead of `` you don't need this
        >
        > > open($k, "+< $hasharray{$k}");
        > > @ARRAY1 = <$k>;
        > > foreach $element (@ARRAY1) {
        > > if ($element=~/^(.*Access.*)$/) {
        > > print "$hasharray{$k} : $element\n";
        > > }
        > > }
        > > seek($k,0,0);
        > > print $k @ARRAY1;
        > > truncate($k,tell($k));
        > > close($k);
        > > }
        > >
        > > Without massively abusing me for my code can you tell me what I have
        > > to do to be able to run this with strict refs?
        >
        > Since you do not keep several files opened at once you do not need
        > the hash and you do not need to use several different filehandles.
        > You can replace both loops by:
        >
        > foreach my $file (@ARRAY) {
        > open(my $FH, "+< $file");
        > my @ARRAY1 = <$FH>;
        > foreach my $element (@ARRAY1) {
        > if ($element=~/^(.*Access.*)$/) {
        > print "$file : $element\n";
        > }
        > }
        > seek($FH,0,0);
        > print $FH @ARRAY1;
        > truncate($FH,tell($FH));
        > close($FH);
        > }
        >
        >
        > Please do consider using better variable names!
        > @ARRAY and @ARRAY1 are not very descriptive.
        > @files and @elements would be much better.
        >
        > HTH, Jenda
        > ===== Jenda@... === http://Jenda.Krynicky.cz =====
        > When it comes to wine, women and song, wizards are allowed
        > to get drunk and croon as much as they like.
        > -- Terry Pratchett in Sourcery
        >
        >
        >
        > Unsubscribing info is here:
        > http://help.yahoo.com/help/us/groups/groups-32.html
        >
        >
        > ------------------------------------------------------------------------
        > Yahoo! Groups Links
        >
        > * To visit your group on the web, go to:
        > http://groups.yahoo.com/group/perl-beginner/
        >
        > * To unsubscribe from this group, send an email to:
        > perl-beginner-unsubscribe@yahoogroups.com
        > <mailto:perl-beginner-unsubscribe@yahoogroups.com?subject=Unsubscribe>
        >
        > * Your use of Yahoo! Groups is subject to the Yahoo! Terms of
        > Service <http://docs.yahoo.com/info/terms/>.
        >
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.