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

Hi Support, I have created a perl script that read a configuration file and extract from it one field if pattern match (field separator is ;;) If i run it on Linux host it works corretcly but if i try to run the script on Windows one it fails (it does not print $1 value) The perl version is for both environment v5.8.8. This an example of entry in configuration file:
hostname1;;/tmp/fab/app01/log/app01.log /tmp/fab/app01/log/app02.stder +r /tmp/fab/app01/log/app03.stdout;; hostname2;;C:\temp\log\loga.txt C:\temp\log\logb.txt;;
#!/opt/OV/contrib/perl/bin/perl #Set my variables my $in_file = $ARGV[0]; #my $in_file = 'configuration_logfile_weblogic'; open my $in_fh, '<', $in_file or die "Could not open file $in_file: $! +"; my $fqdn = `hostname`; @hostname_short = split(/\./,$fqdn,2); my $hostname = "@hostname_short[0]"; $hostname=~ tr/A-Z/a-z/; while ( my $line = <$in_fh> ) { if($line =~ m/^$hostname;;(.*);;/) { print $1; } } close $in_fh or die "Could not close file $in_file: $!";
Can you help me ? Thanks. Fabrizio

Replies are listed 'Best First'.
Re: Perl - pattern matching
by hazylife (Monk) on Mar 31, 2014 at 11:36 UTC
    if i try to run the script on Windows one it fails
    And the fqdn of that Windows host is just one single word, right? Try chomping it like this:
    chomp(my $fqdn = `hostname`); # OR, much better: my ($hostname) = `hostname` =~ /([-a-zA-Z0-9]+)/; $hostname=~ tr/A-Z/a-z/;
    update: no, ([^.]+) won't do

      Hi,

      may I suggest to use the following CORE module (even no addon installation necessary):

      use Sys::Hostname; my $hostname = hostname;

      No subprocess is spawned and hopefully all OS dependent aspects considered. IMHO a much better approach.

      best regards
      McA

      Great now it works ! Thanks a lot ! Fabrizio
Re: Perl - pattern matching
by kcott (Archbishop) on Mar 31, 2014 at 14:01 UTC

    G'day Fabrizio,

    Welcome to the monastery.

    Here's a couple of points unrelated to your "pattern matching" query.

    I see you've attempted to use lexical variables (with my) throughout your code — which is good. Had you used this line at the top of your code:

    use strict;

    The 'vars' stricture of strict would have alerted you to the fact that you'd forgotten to do this for @hostname_short.

    Had you also used this line at the top of your code:

    use warnings;

    The 'syntax' category of warnings would have emitted a message that actually gave you a better way to write @hostname_short[0]:

    Scalar value @hostname_short[0] better written as $hostname_short[0]

    Using both of these pragmata in all of your scripts is a very good idea.

    As it turns out, you could have written both of these lines:

    @hostname_short = split(/\./,$fqdn,2); my $hostname = "@hostname_short[0]";

    as just this single line:

    my $hostname = (split /\./, $fqdn, 2)[0];

    -- Ken

      Thanks!
        Hi, I continue working on my script and now is more powerfull :) ! I have a question about this section
        chomp (my $OVINSTDIR = `echo %OvInstallDir%`); if(-e $OVINSTDIR.'\bin\win64\opcmon.exe'){ my $COMMAND = $OVINSTDIR.'\bin\win64\opcmon.ex +e'; $OPCMON="$COMMAND"; qx[$OPCMON]; }
        when i execute the command $OVINSTDIR.'\bin\win64\opcmon.exe' i got error: 'C:\Program' is not recognized as an internal or external command, because variable $OVINSTDIR contains space (C:\Program Files\HP\HP BTO Software\) How can i manage this behavior ? Thanks, Fabrizio