Parsing a variable that has spaces in perl

I am able to parse a variable from a perl script to another script using the example URL below, except when the variable has a space in it, like "Jane Doe"

I'm using:
script.php?variable1=$variable1

but when variable1 = "Jane Doe" -- only the "Jane" portion is parsed, leaving out anything after the space, ie "Doe"

How can I pass the whole variable with spaces?
webguyfredAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Todd MummertCommented:

/\?([^=]+)=([^&]+)/

basically split on =   terminate the 2nd match with & or end of string

If your variable is in quotes


/\?([^=]+)=("[^"]*")/
0
webguyfredAuthor Commented:
Can you elaborate a bit as i'm a perl newbie. I was hoping to be able to modify the url to include the spaces in the variable:

script.php?variable1=$variable1

if not possible to do that in the url, would I just insert the code you mention in the script where  the variable is defined?

thanks
0
Todd MummertCommented:

ah..you want to create the url string:

here is one way, the 2nd argument is the base string

use URI::URL;
my $u1 = URI::URL->new("variable1=$variable1", "script.php?");
print $u1->abs,"\n";

0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Todd MummertCommented:

Or if you just want to handle spaces, which convert to %20 in URLs

my $string = "script.php?variable1=$variable1";
$string =~ s/ /%20/g;
print $string;


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mrjoltcolaCommented:
climbgunks is correct, you need to URL encode the string. You can either use URI::URL, or do this..
use URI::Escape;
 
my $url = url_enscape("script.php?variable1=$variable1");
 
print "URL encoded: $url\n";

Open in new window

0
webguyfredAuthor Commented:
thanks for your expertise
0
webguyfredAuthor Commented:
thanks both for the comments, climbgunks suggestion worked like a charm:

my $string = "script.php?variable1=$variable1";
$string =~ s/ /%20/g;
print $string;
0
Adam314Commented:
That suggestion works for spaces, but it would not work for other special characters.  You would be better off using the URI::URL module as suggested in post 24487667, or the URI::Escape module in post 24487758, although it has a typo - the function should be:
    my $url = uri_escape("script.php?variable1=$variable1");

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.