CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
File printing mixed up order in foreach (CGI)

 



_Chipwiz_Ben_
Novice

Mar 7, 2011, 3:29 AM

Post #1 of 2 (1565 views)
File printing mixed up order in foreach (CGI) Can't Post

Hello all,

I have a little problem with the ordering of text when printed to a file. Here's the problematic code:


Code
$USERNAME_INPUT = $cgi->param("USERNAME_INPUT"); 
$PASSWORD_INPUT = $cgi->param("PASSWORD_INPUT");
$SERVICE_INPUT = $cgi->param("SERVICE_INPUT");
$PROTOCOL_INPUT = $cgi->param("PROTOCOL_INPUT");
$ADDRESS_INPUT = $cgi->param("ADDRESS_INPUT");
$ARGUMENT_INPUT = $cgi->param("ARGUMENT_INPUT");
$ADDITIONAL_INPUT = $cgi->param("ADDITIONAL_INPUT");

if (($USERNAME_INPUT ne '') && ($PASSWORD_INPUT ne '') && ($SERVICE_INPUT ne '') && ($PROTOCOL_INPUT ne '') && ($ADDRESS_INPUT ne '') && ($ARGUMENT_INPUT ne '')) {
&write_to_users
}

sub write_to_users {
open(USERS, '>>', $USERSFILE) or die $!;
print USERS "

$USERNAME_INPUT User-Password==\"$PASSWORD_INPUT\"
Service-Type = $SERVICE_INPUT,
Framed-Protocol = $PROTOCOL_INPUT,
Framed-Address = $ADDRESS_INPUT,
Cisco-AVpair = \"$ARGUMENT_INPUT\"";


foreach $line ( split(/\n/, $ADDITIONAL_INPUT) ){

print USERS ",
Cisco-AVpair += \"$line\"";
}

} # sub write_to_users {

close USERS;


To cut a long story short, it's to append blocks of text to the end of a radius users.tcdsl. If you don't know what that is it doesn't matter, then problem is with basic text output to the file. The variables are filled in a CGI form and posted back to the same file. What it should look like is this:


Code
user@domain.com User-Password=="mypassword" 
Service-Type = Framed-User,
Framed-Protocol = PPP,
Framed-Address = 10.10.10.10,
Cisco-AVpair = "ip:route=123.123.123.123 255.255.255.240",
Cisco-AVpair += "ip:route=192.168.88.0 255.255.255.0",
Cisco-AVpair += "lcp:interface-config= ip vrf forwarding V112:EXAMPLE",
Cisco-AVpair += "lcp:interface-config= ip unnumbered Loopback112"


But what is actually looks like is this:


Code
user@domain.com User-Password=="mypassword" 
Service-Type = Framed-User,
Framed-Protocol = PPP,
Framed-Address = 10.10.10.10,
Cisco-AVpair = "ip:route=123.123.123.123 255.255.255.240",
", Cisco-AVpair += "ip:route=192.168.88.0 255.255.255.0
", Cisco-AVpair += "lcp:interface-config= ip vrf forwarding V112:EXAMPLE
Cisco-AVpair += "lcp:interface-config= ip unnumbered Loopback112"


All lines starting with 'Cisco-AVpair =+' come from a textarea because there are varying number of lines depending on the connection config. In some cases there is no 'Cisco-AVpair =+' at all.

The problem, as I'm sure you can see, is that the ", that's supposed to go after the string is placed before it - it has totally baffled me as to why this is happening. By removing code I've traced the problem back to $line - it's this that seems to be causing the problem somehow.

Is this a bug in Perl or am I doing something wrong?

Thanks to all that reply,
Ben


_Chipwiz_Ben_
Novice

Mar 7, 2011, 4:25 AM

Post #2 of 2 (1560 views)
Re: [_Chipwiz_Ben_] File printing mixed up order in foreach (CGI) [In reply to] Can't Post

I fixed it with:

$line =~ s/\r//g;

Didn't expect line returns to be present after a foreach $line.

That'll teach me for making assumptions.

Cheers,
Ben

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives