0

How do I pass a variable into a URL in a Perl script? I am trying to pass the variables in an array to url. For some reason it is not working. I am not sure what I am doing wrong. The code roughly looks like this:

@coins = qw(Quarter Dime Nickel);

foreach (@coins) {
  my $req = HTTP::Request->new(POST =>'https://url/$coins.com');
 } 

This does not work as $coins does not switch to Quarter,Dime,Nickel respectively.

What am I doing wrong?

1
  • To point you in the right direction: $coins != @coins in your example above. Commented Oct 21, 2010 at 12:36

2 Answers 2

7

First, variables do not interpolate in single quoted strings:

my $req = HTTP::Request->new(POST => "https://url/$coins.com");

Second, there is no variable $coins defined anywhere:

foreach my $coin (@coins) {
  my $req = HTTP::Request->new(POST => "https://url/$coin.com");
 }

Also, make sure to use strict and warnings.

You should also invest some time into learning Perl properly.

Sign up to request clarification or add additional context in comments.

2 Comments

Also, name the scalar value $coin, not $coins.
Or without having a loop variable, you may use $coin[n] or $_ in place of $coin
1

Use

'https://url/' . $_  . '.com'

Instead of your

'https://url/$coins.com'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.