Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use # sign as an input in perl script

Status
Not open for further replies.

yuenli

Programmer
Dec 30, 2008
3
MY
Hi! I am editting a perl script that will convert html to MIF file. There is a section in the script where it will match html code and translate it into MIF file symbol code such as ($astr =~ s/®/\\xe4 /g;). How do I use # in this code such as ($astr =~ s/Ω/\\x57 /g;) as # is a comment symbol?
 
Try it and see what it does. :)

Code:
[kirsle@eternity ~]$ perl
my $str = "testingΩsomething\n";
my $new = $str;
$new =~ s/Ω/\\x57/g;
print "in: $str\n";
print "out: $new\n";
__END__
in: testingΩsomething

out: testing\x57something

Perl knows when to do the right thing. ;)

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
The # symbol is a comment inside perl code, but not in input supplied to your perl code. Or maybe I am missing something.

Code:
$_ = '#';#<-- the # in the string is not a comment
s/#/foo/;#<-- not a comment here either
print; # prints 'foo'

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I don't think that __END__ is a valid section actually. For instance:

Code:
[COLOR=blue][kirsle@eternity ~]$[/color] cat end.pl 
print "Testing comments in the END section\n";
while (<END>) {
        print;
}
__END__
Hello world!
Hello mars!
print "test"; # prints "test"
my $string = "#5";
my $regexp =~ s/#/pound/ig;
[COLOR=blue][kirsle@eternity ~]$[/color] perl end.pl 
Testing comments in the END section
[COLOR=blue][kirsle@eternity ~]$[/color] cat data.pl 
print "Testing comments in the DATA section\n";
while (<DATA>) {
        print;
}
__DATA__
Hello world!
Hello mars!
print "test"; # prints "test"
my $string = "#5";
my $regexp =~ s/#/pound/ig;
[COLOR=blue][kirsle@eternity ~]$[/color] perl data.pl 
Testing comments in the DATA section
Hello world!
Hello mars!
print "test"; # prints "test"
my $string = "#5";
my $regexp =~ s/#/pound/ig;

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
__END__ is not a filehandle like __DATA__ is.

I think __END__ is mostly used by AutoLoader and SelfLoader or just to tell perl where the end of the program is. It might also have differnt uses if it is used in main or a module. I'd have to read the perl documentation to refresh my memory but anyone can read the documentation and find out if they are interested. I think in main __END__ is the same as __DATA__ so something like this should work:

Code:
print "Testing comments in the END section\n";
while (<DATA>) {
        print;
}
__END__
Hello world!

but not:

Code:
print "Testing comments in the END section\n";
while (<END>) {
        print;
}
__END__
Hello world!

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
but in *.fm file when I used # in the script it will treat it as a comment symbol. How to solve this? Thanks.
 
Do you have example code of this happening? If Perl was treating it as a comment, it would also be throwing a syntax error most likely, because:

Code:
$something =~ s/&[COLOR=blue]#8486;/\\x57/g;[/color]

[COLOR=red]$something =~ s/&[/color]

Perl would complain about the unterminated regexp and/or about the lack of a semicolon on that line (especially if other lines follow it), etc.

If ever in doubt about a character just escape it: \#

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
If a # symbol is being treated like a perl comment then the input is being parsed as perl code and not treated as program input.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Yes it did throw out error if I use $astr =~ s/&#8486;/\\x57 /g; I did try to use $astr =~ s/'&#8486;'/\\x57 /g; but the output result does not replace &#8486; to \\x57. It will still show &#8486;

Appreciate if anyone can help me. Thanks.
 
This doesn't give me any errors:

Code:
$something = '&#8486;';
$something =~ s/&#8486;/\\x57/g;
print $something;


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top