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

Is there a way of doing such a thing. I have a list of URLS that I print using a format, however when one contains an @ (eg. user:pass@site.domain) the script fails. Is there a way of escaping the @ or ^ characters so that it treats them as normal characters?

Replies are listed 'Best First'.
RE: Escaping an @ or a ^ in a format
by setantae (Scribe) on Feb 08, 2000 at 07:41 UTC
    This seems to work OK:
    [setantae@archaia setantae]$ cat barf #!/usr/bin/perl -w $normal = "text here"; $at = "setantae\@eidosnet.co.uk"; $caret = "something^with a caret in"; write STDOUT; format STDOUT = @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<< +<<< $at, $caret, $normal . [setantae@archaia setantae]$ ./barf setantae@eidosnet.co.uk something^with a caret in text h +ere
    You need to escape the @ (which -w would have told you (yeah, yeah, no use strict;, but I'm trying to keep it short)).
    setantae@eidosnet.co.uk|setantae|http://www.setantae.uklinux.net
Re: Escaping an @ or a ^ in a format
by Anonymous Monk on Feb 09, 2000 at 17:06 UTC
    I actually thought of using the code that you suggested:
    format Ident = I have an @ here. "@" .
    But then I decided to just print the URLS in a format field anyways (even though a format is not needed as the urls will be within td tags) I was just interested that when invoking a format there seems to be no way of escaping the @ or ^ symbols apart from using a format. Of course what I wrote is not really the purpose of using a format.. but oh well..
RE: Escaping an @ or a ^ in a format
by jamieamieamily (Initiate) on Feb 08, 2000 at 07:54 UTC
    Sory, I should have included more information. What I am wanting to do is something along the lines of this: #!/usr/bin/perl -w $normal = "text here"; $at = "setantae\@eidosnet.co.uk"; $caret = "something^with a caret in"; write STDOUT; format STDOUT = login1 is jamie@site1.com login2 is jamie@site2.com this is a caret: ^ more text here @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<< $at, $caret, $normal . The lines containing the @ and the ^ are printed as a header.
      Hmm, any chance you could rewrite this using CODE tags - it'll help
      Check out Perl Monk procedures for more info.
Re: Escaping an @ or a ^ in a format
by jamieamieamily (Initiate) on Feb 08, 2000 at 08:07 UTC
    With tags:
    #!/usr/bin/perl -w 
    $normal = "text here"; 
    $at = "setantae\@eidosnet.co.uk"; 
    $caret = "something^with a caret in"; 
    write STDOUT; 
    format STDOUT = 
    login1 is jamie@site1.com 
    login2 is jamie@site2.com 
    this is a caret: ^ 
    more text here 
    @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<< 
    $at, $caret, $normal
    
      Hmm, not quite sure what you're trying to do - fancy posting the code you have?
Re: Escaping an @ or a ^ in a format
by btrott (Parson) on Feb 08, 2000 at 23:04 UTC
    Apparently (according to perlform), you can escape a "^" or a "@" like this:
    format Ident = I have an @ here. "@" .
    That said, though, I'm not sure that you want to be writing normal (non-formatted data) into the format.

    According to perlform, one format line should contain either: a comment; a "picture" line describing a format; an argument line giving the values to plug into the previous picture line.

    So perhaps you should just use print statements, or investigate the use of format headers, instead?

      A followup: when I wrote to use print statements "instead", I meant instead of the non-formatted data (like header information); you can alternate print and write statements, I believe.
Re: Escaping an @ or a ^ in a format
by jamieamieamily (Initiate) on Feb 08, 2000 at 08:59 UTC
    Basically I have written two perl modules that print a report presenting urls and the number of hits per url. The first is a text based format, the second prints html. As i used td tags in the html version i did not need to create fields for the url strings, which is when i ran into this problem. As the @ in the url string is treated as text when an eval is done it expects input. Here is a cut down version:
    #!/usr/bin/perl %hash = qw(site.com 10 jamie@site.com:site1.com 4 user@hots:foo.bar 20 +); eval print_url_info(\%hash,10,"STDOUT"); write; exit 0; sub print_url_info { my ($url_data, $num_urls, $fh)=@_; my ($format)="", ($header)="$num_urls Most Popular URLs :", ($local_debug)=$debug, ($index)=0; ############## # Geneate a string to be eval'd containing the most popular x urls + for this user $format = "format $fh =\n"; $format = $format . "<!--- Start User Top URLS Section -->\n"; $format = $format . "<font size=3><u>$header</u></font><br><br>\n" +; $format = $format . "<table border=0>\n <tr>\n"; $format = $format . " <th align=left>URL</th>\n"; $format = $format . " <th width=10></th>\n"; $format = $format . " <th align=left>Num of Hits</th>\n +</tr>\n"; ############## # Print the urls in descending order $index=0; foreach (reverse sort { ${$url_data}{$a} <=> ${$url_data}{$b} } keys %{$url_data} ) { $format = $format . " <tr>\n"; $format = $format . " <td align=left>$_</td>\n"; $format = $format . " <td align=right> ${$url_data}{$_} + </td>\n </tr>\n"; ( ++$index == $num_urls ) && last; } $format = $format . "</table>\n<br>\n"; $format = $format . "<!--- End User Top URLS Section -->\n"; $format = $format . "\n.\n"; return $format; }
    I am now using place holders to print the URLs which is ultimately preferrable however I would still like to find out if an @ or a ^ can be treated and printed as normal text in a format.
Re: Escaping an @ or a ^ in a format
by jamieamieamily (Initiate) on Feb 08, 2000 at 09:08 UTC
    Attempt with html tags, though you can see the source by viewing this html page and the previous post
    
    #!/usr/bin/perl
    
    %hash = qw(site.com 10 jamie@site.com:site1.com 4 user@hots:foo.bar 20);
    
    eval print_url_info(\%hash,10,"STDOUT");
    write;
    exit 0;
    
    
    sub print_url_info
    {
    	my ($url_data,
    	    $num_urls,
    	    $fh)=@_;
    
    	
    	my ($format)="",
    	   ($header)="$num_urls Most Popular URLs :",
    	   ($local_debug)=$debug,
    	   ($index)=0;
    
    
    	##############
    	# Geneate a string to be eval'd containing the most popular x urls for this user
    
    
    	$format = "format $fh =\n";
    	$format = $format . "<!--- Start User Top URLS Section -->\n";
    
    	$format = $format . "<font size=3><u>$header</u></font><br><br>\n";
    
    	
    	$format = $format . "<table border=0>\n	<tr>\n";
    	$format = $format . "		<th align=left>URL</th>\n";
    	$format = $format . "		<th width=10></th>\n";
    	$format = $format . "		<th align=left>Num of Hits</th>\n	</tr>\n";
    
    
    	##############
    	# Print the urls in descending order
    
    	$index=0;
    	foreach (reverse 
    		 sort { ${$url_data}{$a} <=> ${$url_data}{$b} } 
    		 keys %{$url_data} )
    	{
    		$format = $format . "	<tr>\n";
    		$format = $format . "		<td align=left>$_</td>\n";
    		$format = $format . "		<td align=right> ${$url_data}{$_} </td>\n	</tr>\n";
    
    		( ++$index == $num_urls ) && last;
    	}
    
    	$format = $format . "</table>\n<br>\n";
    
    	$format = $format . "<!--- End User Top URLS Section -->\n";
    
    	$format = $format . "\n.\n";
    
    	return $format;
    
    }