[Perl] Printing - Scalars

Hey Guys,

I have some weird problem with printing scalars ...
When I'm executing script both are printing in terminal ...
But only one is printed to the file ?

I don't know whats going on .. :slight_smile:

Btw .. I'm noobie :slight_smile: took me lots of time to put this simple script together :slight_smile:

Thank you in advance

#!/usr/bin/perl 


 open (FILE, ">data.txt"); 
 open (FILE2, ">data2.txt"); 

 $content = get("http://lcoalhost/test.html");
 die "Couldn't get it!" unless defined $content;

($plain_text = $content) =~ s/<[^>]*>//gs;    


 $mystringa = "$plain_text";
if($mystringa =~ m/Start(.*?)End/) {
    print $1;
}



 $mystringb = "$plain_text";
if($mystringb =~ m/Word(.*?)\(/) {
    print $2;
}


print "$1\n";
print "$2\n";

print FILE $1; 
print FILE $2; 
print FILE2 "$plain_text"; 


$1 and $2 are quite volatile variables, in the sense that they only start being defined after a regex with matching groups, and that they'll have a different value after the next one. Better save it to a new variable as soon as possible after the regex.

Aside from that, this is always a good idea at the beginning of your script:

#!/usr/bin/perl -W

use strict;
use warnings;

This will enable strict variable declaration (using my or our) and a few warnings that might turn into bugs down the road.

Hey,

Thank you ...

Global symbol "$content" requires explicit package name at test2.pl line 10.
Global symbol "$content" requires explicit package name at test2.pl line 11.
Global symbol "$plain_text" requires explicit package name at test2.pl line 13.
Global symbol "$content" requires explicit package name at test2.pl line 13.
Global symbol "$mystringa" requires explicit package name at test2.pl line 16.
Global symbol "$plain_text" requires explicit package name at test2.pl line 16.
Global symbol "$mystringa" requires explicit package name at test2.pl line 17.
Global symbol "$mystringb" requires explicit package name at test2.pl line 23.
Global symbol "$plain_text" requires explicit package name at test2.pl line 23.
Global symbol "$mystringb" requires explicit package name at test2.pl line 24.
Global symbol "$plain_text" requires explicit package name at test2.pl line 34.

Not sure how to fix it ?
Could you please post some example of re saving variables

Thx

Example 1: declaring variables

use strict;
my $var1 = "Test 1"; #Good
$var2 = "Test 2"; #This will fail

Example 2: saving variables

use strict;
my $string = "This is a test";
$string =~ m/ (..) /g;
my $save = $1;
print $save, "\n"; # Prints "is", followed by a newline

Also, there's a lot of documentation on perldoc.perl.org, including some where fine tutorials. All documentation there can also be found on your local system using the "perldoc" command.

HTH